﻿using AvalSqlIndexManager.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace AvalSqlIndexManager.Models
{
    internal class TableStorage
    {
        private Dictionary<string, List<TableIndex>> storage;
        private Dictionary<string, NodeViewModel> tableNodeViewModels;
        private ObservableCollection<NodeViewModel> lastNodeViewModels;
        public TableStorage()
        {
            storage = new Dictionary<string, List<TableIndex>>();
            tableNodeViewModels = new Dictionary<string, NodeViewModel>();
            lastNodeViewModels = new ObservableCollection<NodeViewModel>();
        }

        private void AddTableNodeViewModel(TableIndex table)
        {
            if (!tableNodeViewModels.ContainsKey(table.TableName))
            {
                NodeViewModel NodeViewModel = new NodeViewModel(table.TableName, true);
                lastNodeViewModels.Add(NodeViewModel);                
                tableNodeViewModels.Add(table.TableName, NodeViewModel);
            }
            tableNodeViewModels[table.TableName].AddNode(table);
        }

        public void AddIndex(TableIndex table)
        {
            string tableName = table.TableName;
            if (string.IsNullOrEmpty(tableName))
                return;
            if (!storage.ContainsKey(tableName))
                storage.Add(tableName, new List<TableIndex> { table });
            else
                storage[tableName].Add(table);
            AddTableNodeViewModel(table);
        }
        public ObservableCollection<NodeViewModel> TableNodes { get { return lastNodeViewModels; } }
        private void buildNodeViewModels()
        {
            lastNodeViewModels.Clear();
            tableNodeViewModels.Clear();
            foreach (KeyValuePair<string, List<TableIndex>> pair in storage)
            {
                foreach (TableIndex table in pair.Value)
                {
                    AddTableNodeViewModel(table);
                }
            }
        }
        public static TableStorage FromFile()
        {
            var store = new TableStorage();
            string fullFile = getFileFullName();
            if (System.IO.File.Exists(fullFile))
            {
                string json = File.ReadAllText(fullFile);
                var result = JsonHelper.Deserialize<Dictionary<string, List<TableIndex>>>(json);
                if (result != null)
                    store.storage = result;
            }
            store.buildNodeViewModels();
            return store;
        }

        public void Save()
        {
            string fullFile = getFileFullName();
            var file = new FileInfo(fullFile);
            if (!file.Directory!.Exists)
                file.Directory.Create();
            string json = JsonHelper.Serialize(storage);
            File.WriteAllText(fullFile, json);
        }

        private static string getFileFullName()
        {
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db", "index.json");
        }
    }
}
